// pages/cardbark/personaldata/personaldata.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgurl = app.globalData.imgurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
   isFriend:0, //0为不是好友，1为是好友
   edit:0//0为可编辑 1为不可编辑
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    if (options.id){
      that.setData({
        edit:1
      })
    }else{
      that.setData({
        edit: 0
      })
    }
    console.log(options.id)
    var id = options.id ? options.id : wx.getStorageSync("list").guestid
    that.setData({
      id:id
    })
    that.getmyselfinfo(id)
    that.isFriend()
  },
  sendmsg:function(){
    wx.navigateTo({
      url: '',
    })
  },
  // 获取个人资料
  getmyselfinfo: function (id) {
    var that = this
    app.util.request({
      url: testAddr + "Guest/GetInfoByID?guestID=" + id,
      method: "POST",
      data: {},
      success: function (res) {
        var useInfo = res.data
        if (useInfo.imgUrl) {
          useInfo.imgUrl = imgurl + "/guest/" + useInfo.imgUrl
        }
        that.setData({
          infodetail: useInfo
        })
        console.log(res)
      }
    })
  },
  // 发信息
  into_chatRoom: function (event) {
    var my = wx.getStorageSync("myUsername");
    var nameList = {
      myName: my,
      your: event.currentTarget.id,
      head: event.currentTarget.dataset.head
    };
    console.log(nameList)
    wx.navigateTo({
      url: "../../detail/chatroom/chatroom?username=" + JSON.stringify(nameList)
    });
  },
  // 判断是否为好友
  isFriend:function () {
    var that = this
    var my = wx.getStorageSync("myUsername");
    var your = that.data.id
    app.util.request({
      url: testAddr + "/linkman/isFriend?guestID=" + my + "&linkID=" + your,
      method: "POST",
      data: {},
      success: function (res) {
       console.log(res)
        if (res.data.result==0){
          that.setData({
            isFriend:0
          })
        }else{
          that.setData({
            isFriend: 1
          })
        }
      }
    })
  },
  // @RequestParam Integer guestID（自己的ID）
  // @RequestParam Integer linked(好友ID)
  // @RequestParam(required = false) String description, （描述）
  // @RequestParam(required = false) Integer linkType（传1过来就可以）

  // 加为好友
  addFriend: function (event){
    var that = this
    var my = wx.getStorageSync("myUsername");
    var your = event.currentTarget.id
    app.util.request({
      url: testAddr + "/linkman/addFriend?guestID=" + my + "&linkID=" + your + "&description=" + "请求加为好友" + "&linkType=" + 1,
      method: "POST",
      data: {},
      success: function (res) {
        wx.showToast({
          title: res.data.result,
          icon:'none'
        })
        console.log("请求成功~")
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})